package com.example.controller;

import com.example.common.Result;
import com.example.pojo.Appointment;
import com.example.service.AppointmentService;
import com.example.util.JwtUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    @Resource
    private AppointmentService appointmentService;


    /**
     * 未处理的预约数量
     * @return
     */
    @GetMapping("/unProcessedCount")
    public Result unProcessedCount(Integer ownerId) {
        int count = appointmentService.unProcessedCount(ownerId);
        return Result.success(count);
    }
    /**
     * 添加预约
     * @param appointment
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Appointment appointment) {
        appointmentService.add(appointment);
            return Result.success();
    }

    @GetMapping("/checkTime")
    public Result checkTime() {
        LocalDateTime now = LocalDateTime.now();
        Date now2 = new Date();
        return Result.success("LocalDateTime: "+now+"\tDate: "+now2);
    }

    /**
     * 删除预约
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer userId,@RequestParam Integer roomId) {
        appointmentService.deleteById(userId,roomId);
        return Result.success();
    }

    @PutMapping("/confirm")
    public Result confirm(@RequestParam Integer id) {
        appointmentService.confirmAppointment(id);
        return Result.success("200","确认预约成功");
    }

    /**
     * 取消预约
     * @param id
     * @param cancelReason
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestParam Integer id,@RequestParam String cancelReason) {
        appointmentService.cancelAppointment(id,cancelReason);
        return Result.success("200","取消预约成功");
    }



    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询预约
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Appointment appointment = appointmentService.selectById(id);
        return Result.success(appointment);
    }

    /**
     * 查询所有预约
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(@RequestHeader("Authorization") String token) {
        Map<String, Object> claims = JwtUtil.parseToken(token);
        Integer userId = (Integer) claims.get("id");
        String role = (String) claims.get("role");
        List<Appointment> list = appointmentService.selectAll(userId,role);
        return Result.success(list);
    }



    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(Appointment appointment,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Appointment> pageInfo= appointmentService.selectPage(appointment,pageNum,pageSize);
        return Result.success(pageInfo);
    }

}
